package com.fei.fpmall.malladmin.controller;

import com.fei.fpmall.core.dto.ResponseModel;

import com.fei.fpmall.core.model.Page;
import com.fei.fpmall.entity.basemanage.User;
import com.fei.fpmall.entity.basemanage.UserdetailEntity;
import com.fei.fpmall.entity.bcompany.BcompanyEntity;
import com.fei.fpmall.service.basemanage.IUsersService;
import com.fei.fpmall.service.bcompany.IBcompanysService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @program: fpmall
 * @description: 开票税号管理控制器
 * @author: felix
 * @create: 2018-04-03 10:31
 **/
@Controller
@RequestMapping("/bcompany")
public class BCompanyController extends BaseController {

    @Resource
    IBcompanysService bcompanysService;

    @Resource
    IUsersService usersService;

    //列表
    @RequestMapping("/list")
    public  String BCompanyList(Model model){

        Page page = new Page(request);
        //查询条件
        Map condition=new HashMap<String,String>();
        ResponseModel responseModel= bcompanysService.queryListByPage(condition,page);
        model.addAttribute("model",responseModel);
        java.util.Map bCStateList=new java.util.Properties();
        bCStateList.put("全部","-1");
        bCStateList.put("正常","1");
        bCStateList.put("关闭","0");
        model.addAttribute("bCStateList",bCStateList);
        return "bcompany/list";
    }

    //新增
    @RequestMapping(value="/add",method = RequestMethod.GET)
    public String BcompanyAdd(Model model) {

        return "bcompany/add";
    }

    //新增保存
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    public Object BcompanySave(BcompanyEntity bcompanyEntity) {

        if(bcompanyEntity == null){
            return new ResponseModel(-2);
        }
        if(StringUtils.isEmpty(bcompanyEntity.getIndustrycode())){
            return new ResponseModel(-1,"行业编码不能为空");
        }
        if(StringUtils.isEmpty(bcompanyEntity.getBcname())){
            return new ResponseModel(-1,"名称不能为空");
        }
        bcompanysService.save(bcompanyEntity);
        return  new ResponseModel(0,"保存成功");
    }

    //选择开票管理员
    @RequestMapping(value="/managerselect/{bcid}",method = RequestMethod.GET)
    public String BCompanyManagerSelectList(Model model,@PathVariable("bcid") int bcid) {

        Page page = new Page(request);
        //查询条件
        Map condition=new HashMap<String,String>();
        condition.put("bcid",0);
        condition.put("mallagid",1);
        condition.put("userstatus",1);
        ResponseModel responseModel= usersService.queryListByPage(condition,page);
        model.addAttribute("model",responseModel);
        model.addAttribute("bcid",bcid);
        return "bcompany/managerselect";
    }

    //设置开票管理员
    @RequestMapping(value = "/managerset/{bcid}_{uid}",method = RequestMethod.GET)
    @ResponseBody
    public Object BCompanyManagerSet(@PathVariable("uid") int uid,@PathVariable("bcid") int bcid){
        User userInfo=usersService.queryObject(uid);
        if (userInfo == null) {
            return new ResponseModel(-2);
        }
        userInfo.setBcid(bcid);
        usersService.update(userInfo);
        return  new ResponseModel(3,"修改成功");
    }

    //更新开票管理员状态
    @RequestMapping(value = "/managerstateupdate/{uid}",method = RequestMethod.GET)
    @ResponseBody
    public Object BCompanyManagerStateUpdate(@PathVariable("uid") int uid,int userstatus){

        User userInfo=usersService.queryObject(uid);
        if (userInfo == null) {
            return new ResponseModel(-2);
        }
        userInfo.setUserstatus(userstatus);
        usersService.update(userInfo);
        return  new ResponseModel(3,"修改成功");
    }

    //更新开票公司状态
    @RequestMapping(value = "/bcompanystateupdate/{bcid}",method = RequestMethod.GET)
    @ResponseBody
    public  Object BcompanyStateUpdate(@PathVariable("bcid") int bcid, int state){

        BcompanyEntity bcompanyEntity= bcompanysService.queryObject(bcid);
        if (bcompanyEntity == null) {
            return new ResponseModel(-2);
        }
        bcompanyEntity.setState(state);
        bcompanysService.update(bcompanyEntity);
        return  new ResponseModel(3,"修改成功");
    }

    //开票管理员列表
    @RequestMapping("/managerlist")
    public  String BCompanyManagerSelectList(Model model){
        Page page = new Page(request);
        //查询条件
        Map condition=new HashMap<String,String>();
        condition.put("mallagid",1);
        ResponseModel responseModel= usersService.queryListByPage(condition,page);
        model.addAttribute("model",responseModel);

        java.util.Map userStautList=new java.util.Properties();
        userStautList.put("全部","-1");
        userStautList.put("启用","1");
        userStautList.put("停用","0");
        model.addAttribute("userStautList",userStautList);

        return "bcompany/managerlist";
    }
    //新增开票管理员
    @RequestMapping(value="/manageradd",method = RequestMethod.GET)
    public String BCompanyManagerAdd(Model model) {
        return "bcompany/manageradd";
    }

    //开票管理员保存
    @RequestMapping(value = "/managersave",method = RequestMethod.POST)
    @ResponseBody
    public Object BCompanyManagerSave(User userInfo, UserdetailEntity userdetailInfo) {

        if(userInfo == null||userdetailInfo==null){
            return new ResponseModel(-2);
        }
        if(StringUtils.isEmpty(userInfo.getUsername())){
            return new ResponseModel(-1,"用户名不能为空");
        }
        if(StringUtils.isEmpty(userInfo.getMobile())){
            return new ResponseModel(-1,"手机号码不能为空");
        }
        if(StringUtils.isEmpty(userdetailInfo.getRealname())){
            return new ResponseModel(-1,"真实名称不能为空");
        }

        //开票管理员组ID
        userInfo.setMallagid(1);
        //同时保存主信息和扩展信息
        usersService.save(userInfo,userdetailInfo);
        return  new ResponseModel(0,"保存成功");
    }

    //开票接口配置
    @RequestMapping(value="/fpapiconfig/{bcid}",method = RequestMethod.GET)
    public String BCompanyFPAPIConfig(Model model,@PathVariable("bcid") int bcid){

        BcompanyEntity bcompanyEntity= bcompanysService.queryObject(bcid);

        model.addAttribute("model",bcompanyEntity);
        return "bcompany/fpapiconfig";
    }

    //开票接口配置保存
    @RequestMapping(value = "/fpapiconfigsave/{bcid}",method = RequestMethod.POST)
    @ResponseBody
    public Object BCompanyFPAPIConfigSave(@PathVariable("bcid") int bcid,String appid_bw){

        BcompanyEntity bcompanyEntity= bcompanysService.queryObject(bcid);
        if (bcompanyEntity == null) {
            return new ResponseModel(-2);
        }
        bcompanyEntity.setAppid_bw(appid_bw);
        bcompanysService.update(bcompanyEntity);
        return  new ResponseModel(0,"保存成功");
    }
}
